package com.atguigu.flink.tableapi;

import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;

import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.descriptors.Csv;
import org.apache.flink.table.descriptors.FileSystem;
import org.apache.flink.table.descriptors.Schema;

/**
 * Created by Smexy on 2023/3/4
 *
 *  读取一个文件，将文件中的数据映射为 Table
 */
public class Demo4_ReadFile
{
    public static void main(String[] args) {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        StreamTableEnvironment tableEnvironment = StreamTableEnvironment.create(env);

        //准备一个连接器的描述
        FileSystem fileSystem = new FileSystem().path("data/cep.txt");

        //表的结构
        Schema schema = new Schema().field("id", "STRING")
                                   .field("ts", DataTypes.BIGINT())
                                    .field("vc", DataTypes.INT());

        tableEnvironment.connect(fileSystem)
                        //表连接的文件的格式
                        .withFormat(new Csv())
                        .withSchema(schema)
                        //为表起一个临时的名字
                        .createTemporaryTable("t1");

        //基于表名获取表对象
        Table table = tableEnvironment.from("t1");

        table.execute().print();


    }
}
